package com.weim.weim.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weim.weim.base.ApiResponse;
import com.weim.weim.entity.UserBaseInfo;
import com.weim.weim.entity.UserBaseInfoRequest;
import com.weim.weim.service.IUserBaseInfoService;
import com.weim.weim.service.testInterface;
import com.weim.weim.util.FaceDetectionUtil;
import com.weim.weim.util.VideoFrameCapture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 用户基础信息表 前端控制器
 * </p>
 *
 * @author ju
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/user-base-info")
public class UserBaseInfoController {

    @Autowired
    IUserBaseInfoService iUserBaseInfoService;

    @Autowired
    testInterface testInterface;

    @Autowired
    FaceDetectionUtil faceDetectionUtil;


    @Autowired
    VideoFrameCapture videoFrameCapture;

    @PostMapping("/v1/test")
    public String getCloudroomInfo() {

        return "ddd";
    }

    @PostMapping("/v1/getUserInfo")
    public String getUserInfo(@RequestBody UserBaseInfoRequest request) {
        UserBaseInfo one = iUserBaseInfoService.getOne(new QueryWrapper<>(request));

        return "ddd";
    }

    @PostMapping("/v1/upload")
    public ApiResponse getUploadId(@RequestPart("file") MultipartFile file) throws Exception {

        try {
            String videoPicture = videoFrameCapture.getVideoPicture(file);
            String s = faceDetectionUtil.faceDetection(videoPicture);
        }catch (Exception e){

        }
        return null;
    }

    public  String encodeImageToBase64Binary(String imagePath) {

        try{

            // 假设你的图片文件路径是"path/to/your/image.jpg"
            File imageFile = new File(imagePath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            // 读取图片文件到字节输出流中
            byte[] buffer = new byte[4096];
            int bytesRead;
            try (FileInputStream fis = new FileInputStream(imageFile)) {
                while ((bytesRead = fis.read(buffer)) != -1) {
                    baos.write(buffer, 0, bytesRead);
                }
            }

            // 使用Base64对字节数组进行编码
            byte[] bytes = baos.toByteArray();
            return Base64.getEncoder().encodeToString(bytes);
        }catch (Exception e){

        }
        return null;
    }


}

